package com.xuzhijie.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.*;
import com.xuzhijie.pms.entity.dto.vehicleDto;
import com.xuzhijie.pms.entity.vo.VehicleVo;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.OwnerMapper;
import com.xuzhijie.pms.mapper.VehicleMapper;
import com.xuzhijie.pms.service.IVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-17
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements IVehicleService {

    @Resource
    private VehicleMapper vehicleMapper;

    @Resource
    private OwnerMapper ownerMapper;

    @Override
    public Result findPage(Integer page,
                           Integer size,
                           Integer vehicleCategory,
                           Integer isNewEnergy,
                           Integer isBinding,
                           Integer syncStatus,
                           String licensePlateNumber) {
        Page<Vehicle> vehiclePage = new Page<>(page, size);
        QueryWrapper<Vehicle> queryWrapper = new QueryWrapper<>();
        Page<Vehicle> result;
        Long total;

        if (!vehicleCategory.equals(0)) {
            queryWrapper.eq("vehicle_category", vehicleCategory);
        }

        if (!isNewEnergy.equals(0)) {
            queryWrapper.eq("is_new_energy", isNewEnergy);
        }

        if (!isBinding.equals(0)) {
            queryWrapper.eq("is_binding", isBinding);
        }

        if (!syncStatus.equals(0)) {
            queryWrapper.eq("sync_status", syncStatus);
        }

        if (StrUtil.isNotBlank(licensePlateNumber)) {
            queryWrapper.like("license_plate_number", licensePlateNumber);
        }


        try {
            result = page(vehiclePage, queryWrapper);
            total = count(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }


        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("records", result.getRecords());
        resultMap.put("total", total);

        return Result.success(resultMap);
    }

    @Override
    public Result unbindingProperty(String id) {
        if (StrUtil.isBlank(id)) {
            return Result.error();
        }
        Vehicle one = getById(id);
        one.setMaster("");
        one.setIsBinding(2);
        updateById(one);

        return Result.success();
    }

    @Override
    public Result createVehicle(vehicleDto vehicleDto) {
        if (BeanUtil.isEmpty(vehicleDto)) {
            return Result.error();
        }
        QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", vehicleDto.getPhone());
        System.out.println(vehicleDto.getPhone());
        Owner owner = ownerMapper.selectOne(queryWrapper);
        if (BeanUtil.isEmpty(owner)) {
            return Result.error();
        }

        Vehicle vehicle = new Vehicle();
        BeanUtil.copyProperties(vehicleDto, vehicle);
        vehicle.setIsBinding(1);
        vehicle.setSyncStatus(1);
        vehicle.setMaster(owner.getId());
        String[] images = vehicleDto.getImages();
        vehicle.setImages(Arrays.toString(images));
        System.out.println(vehicleDto.getImages()[0]);

        save(vehicle);

        return Result.success();
    }

    @Override
    public Result getByOwnerId(String id) {

        QueryWrapper<Vehicle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("master", id);
        List<Vehicle> list = list(queryWrapper);
        List<VehicleVo> volist = new ArrayList<>();
        for (Vehicle vehicle : list) {
            VehicleVo vehicleVo = new VehicleVo(vehicle);
            BeanUtil.copyProperties(vehicle,vehicleVo,"images");
            volist.add(vehicleVo);
        }


        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("vehicleList", volist);
        resultMap.put("total", list.size());
        System.out.println(resultMap);


        return Result.success(resultMap);

    }
}
